package com.ccp.dev.system.util;

import com.ccp.dev.core.basic.api.ICurrentContext;
import com.ccp.dev.core.basic.api.IPosition;
import com.ccp.dev.core.basic.api.ISysOrg;
import com.ccp.dev.core.basic.api.ISysUser;
import com.ccp.dev.core.basic.consts.SystemConst;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.system.model.Position;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.PositionService;
import com.ccp.dev.system.service.SysOrgService;
import com.ccp.dev.system.service.SysTenantDynamicDataService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Locale;

/**
 * 获取信息工具类
 * @author y
 */
@Component
public class CurrentContext implements ICurrentContext {

	@Override
	public ISysUser getCurrentUser() {
		//通过setCurrentUserAccount设置的用户。
			ISysUser sysUser=null;
			SecurityContext securityContext = SecurityContextHolder.getContext();
	        if (securityContext != null) {
	            Authentication auth = securityContext.getAuthentication();
	            if (auth != null) {
	                Object principal = auth.getPrincipal();
	                if (principal instanceof ISysUser) {
	                	sysUser=(ISysUser)principal;
	                }
	            } 
	        }
	        return sysUser;
	}

	@Override
	public Locale getLocale() {
		return new Locale("zh","CN");
	}

	@Override
	public void setLocale(Locale locale) {
	}

	@Override
	public String getCurrentUserId() {
		ISysUser isu=getCurrentUser();
		if(isu!=null) {return isu.getUserId();}
		return null;
	}

	@Override
	public void setCurrentUserAccount(String account) {
	}

	@Override
	public void setCurrentUser(ISysUser sysUser) {
	}

	@Override
	public void setCurrentPos(String posId) {
	}

	@Override
	public ISysOrg getCurrentOrg() {
		SysOrgService sysOrgService=AppUtil.getBean(SysOrgService.class);
		SysOrg sysOrg = sysOrgService.getOrgByUserId(getCurrentUserId());
		return sysOrg;
	}

	@Override
	public IPosition getCurrentPos() {
		PositionService positionService=AppUtil.getBean(PositionService.class);
		Position position=positionService.getDefaultPosByUserId(getCurrentUserId());
		return position;
	}

	@Override
	public String getCurrentPosId() {
		IPosition pos= getCurrentPos();
		if(pos!=null) {return pos.getPosId();}
		return null;
	}

	@Override
	public String getCurrentOrgId() {
		ISysOrg sysOrg= getCurrentOrg();
		if(sysOrg!=null){ return sysOrg.getOrgId();}
		return null;
	}

	@Override
	public String getCurrentUserSkin(HttpServletRequest request) {
		return null;
	}

	@Override
	public void cleanCurUser() {
	}

	@Override
	public void removeCurrentOrg() {
	}

	@Override
	public void clearAll() {
	}

	@Override
	public void removeCurrentUser() {
	}

	@Override
	public boolean isSuperAdmin() {
		SysUser user=(SysUser) getCurrentUser();
		Collection<GrantedAuthority> auths = (Collection<GrantedAuthority>) user.getAuthorities();
		if (auths != null && !auths.isEmpty() && auths.contains(SystemConst.ROLE_GRANT_SUPER)) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isSuperAdmin(ISysUser user) {
		SysUser sysUser=(SysUser)user;
		Collection<GrantedAuthority> auths = (Collection<GrantedAuthority>) sysUser.getAuthorities();
		if (auths != null && !auths.isEmpty() && auths.contains(SystemConst.ROLE_GRANT_SUPER)) {
			return true;
		}
		return false;
	}

	@Override
	public String getCurrentUserTenantId() {
		String userId = getCurrentUserId();
		SysTenantDynamicDataService sysTenantDynamicDataService=AppUtil.getBean(SysTenantDynamicDataService.class);
		return sysTenantDynamicDataService.getTenantIdByDataId(userId);
	}

	@Override
	public String getCurrentUserTenantId(String userId) {
		SysTenantDynamicDataService sysTenantDynamicDataService=AppUtil.getBean(SysTenantDynamicDataService.class);
		return sysTenantDynamicDataService.getTenantIdByDataId(userId);
	}

}
